<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cate extends Model
{
    //获取所有分类
    public static function getCates(){
        $cates = self::OrderBy("sort","Desc")->OrderBy("id","Desc")->get();
        $cates=self::makeCates($cates);
        return $cates;
    }
    //无限分类的处理
    public static function makeCates($data,$pid=0,$level=0){
        static $arr=[];
        foreach($data as $v){
            if($v->pid ==$pid){
                $v->level=$level;
                $arr[]=$v;
                self::makeCates($data,$v->id,$level+1);
            }
        }
        return $arr;
    }
    /**
     * 取指定分类的所有子分类
     */
    public static function getCateIds($id){
        static $ids=[];
        if(empty($ids)){
            $ids[]=$id;
        }
        $data=self::all();
        foreach($data as $item){
            if($item->pid==$id){
                $ids[]=$item->id;
                self::getCateIds($item->id);
            }
        }
        return $ids;
    }
}